package com.xhwl.logistics.converter;

import com.google.common.base.Strings;
import com.xhwl.logistics.bo.VehiclePayRecordBO;
import com.xhwl.logistics.bo.VehiclePayRecordStayTimeBO;
import com.xhwl.logistics.component.VehicleHomeAddressProperty;
import com.xhwl.logistics.po.VehiclePayRecordPO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

//@Component
public class ToVehiclePayRecordBO {

    @Autowired
    private VehicleHomeAddressProperty vehicleHomeAddressProperty;

    public VehiclePayRecordBO toVehiclePayRecordBO(VehiclePayRecordPO vehiclePayRecordPO) {
        VehiclePayRecordBO vehiclePayRecordBO = new VehiclePayRecordBO();

        String plateNo = vehiclePayRecordPO.getPlateNo();
        vehiclePayRecordBO.setPlateNumber(plateNo);

        vehiclePayRecordBO.setCardType(vehiclePayRecordPO.getCardType());

        if (!Strings.isNullOrEmpty(plateNo) && plateNo.length() > 2) {
            vehiclePayRecordBO.setHomeAddress(vehicleHomeAddressProperty.getHomeAddress(plateNo.substring(0, 2)));
        } else {
            vehiclePayRecordBO.setHomeAddress("/");
        }

        vehiclePayRecordBO.setPayTime(vehiclePayRecordPO.getPayTime());

        Date existTimeDate = vehiclePayRecordPO.getExistTime();
        Date enterTimeDate = vehiclePayRecordPO.getEnterTime();
        if (null!=existTimeDate && null!=enterTimeDate) {
            long existTime = existTimeDate.getTime()/1000;
            long enterTime = enterTimeDate.getTime()/1000;
            vehiclePayRecordBO.setVehiclePayRecordStayTimeBO(VehiclePayRecordStayTimeBO.vehiclePayRecordStayTimeBO((int)(existTime-enterTime)));
        }

        vehiclePayRecordBO.setTotalMoney(vehiclePayRecordPO.getTotalMoney());
        vehiclePayRecordBO.setPaidMoney(vehiclePayRecordPO.getPaidMoney());
        vehiclePayRecordBO.setFreeMoney(vehiclePayRecordPO.getFreeMoney());

        return vehiclePayRecordBO;
    }

    public List<VehiclePayRecordBO> toVehiclePayRecordBOList(List<VehiclePayRecordPO> list) {
        return list.stream().map(this::toVehiclePayRecordBO).collect(Collectors.toList());
    }

}
